package com.jqr.activity.myui

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column


import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material3.AssistChip
import androidx.compose.material3.AssistChipDefaults
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardColors
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.FilterChip
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.LargeFloatingActionButton
import androidx.compose.material3.LinearProgressIndicator

import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SmallFloatingActionButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.jqr.activity.myui.ui.pages.ScaffoldPage
import com.jqr.activity.myui.ui.theme.MyUITheme
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyUITheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxWidth(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    MyApp()
                }
            }
        }
    }
}

@Composable
fun MyApp(){
    var navController = rememberNavController()

    NavHost(navController = navController, startDestination ="main"){
        composable("main"){ MyAppScreen(navController)}
        composable("scaffold"){ ScaffoldPage(navController)}

    }



}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyAppScreen(navController: NavController){
    Column {
        FilledTonalButton(onClick = {
            navController.navigate("scaffold")
        }) {
            Text(text = "jump to scaffold")
        }
        Row(verticalAlignment = Alignment.CenterVertically) {
            FloatingActionButton(onClick = { /*TODO*/ }) {

                Icon(Icons.Outlined.Add, contentDescription ="add" )
            }

            SmallFloatingActionButton(onClick = { /*TODO*/ }) {
                Icon(Icons.Outlined.Add, contentDescription ="add" )
            }

            LargeFloatingActionButton(onClick = { /*TODO*/ }, shape = CircleShape) {
                Icon(Icons.Outlined.Add, contentDescription ="add" )
            }

            ExtendedFloatingActionButton(
                onClick = { },
                icon = { Icon(Icons.Filled.Edit, "Extended floating action button.") },
                text = { Text(text = "Extended FAB") },
            )
        }
        Card {
            Text(text = "card")
        }
        Card(
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.surface
            ),
            modifier = Modifier
                .fillMaxWidth()
                .height(40.dp)
                .align(Alignment.CenterHorizontally),
            elevation = CardDefaults.cardElevation(
                defaultElevation = 6.dp
            )

        ) {
            Text(text = "fill card")

        }

        AssistChip(
            onClick = { /*TODO*/ },
            label = {
                Text(text = "辅助标签")
            },
            leadingIcon = {
                Icon(Icons.Filled.Settings,contentDescription = null,Modifier.size(AssistChipDefaults.IconSize))
            }

        )
        var selected by remember {
            mutableStateOf(true)
        }

        FilterChip(
            selected =selected ,
            onClick = {selected=!selected},
            label = { Text(text = "条件辅助标签") },
            leadingIcon ={
                if(selected){
                    Icon(Icons.Outlined.Settings, contentDescription =null )
                }
                else{
                    null
                }
            }

        )

        MiniDialog(onDismissRequest = {})



    }


}

@Composable
fun  MiniDialog(
    onDismissRequest:()->Unit
){
    Dialog(onDismissRequest = onDismissRequest) {
        Card(
            Modifier
                .fillMaxSize()
                .padding(16.dp),
            shape = RoundedCornerShape(16.dp)
        ) {
            LinearDeterminateIndicator()
        }
    }


}

@Composable
fun LinearDeterminateIndicator() {
    var currentProgress by remember { mutableStateOf(0f) }
    var loading by remember { mutableStateOf(false) }
    val scope = rememberCoroutineScope() // Create a coroutine scope

    Column(
        verticalArrangement = Arrangement.spacedBy(12.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.fillMaxWidth()
    ) {
        Button(onClick = {
            loading = true
            scope.launch {
                loadProgress { progress ->
                    currentProgress = progress
                }
                loading = false // Reset loading when the coroutine finishes
            }
        }, enabled = !loading) {
            Text("Start loading")
        }

        if (loading) {

            CircularProgressIndicator(progress = currentProgress,Modifier.fillMaxWidth())
            LinearProgressIndicator(
                progress = currentProgress ,
                modifier = Modifier.fillMaxWidth(),
            )
        }
    }
}

/** Iterate the progress value */
suspend fun loadProgress(updateProgress: (Float) -> Unit) {
    for (i in 1..100) {
        updateProgress(i.toFloat() / 100)
        delay(100)
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyUITheme {
        MyApp()
    }
}